package com.ronny;

import java.text.DecimalFormat;


public class Triangle {

	public String validateTriangle(double[] iArrayTSides){

		String result="invalid triangle";

   		if(checkForValidSides(iArrayTSides)){
   			result = determineTriangleType(iArrayTSides);
   		}
   		
   		System.out.println(result);
   		
   		return result;
	}	
	
	private String determineTriangleType(double[] iArrayTriangleSide) {
		
		//DecimalFormat df = new DecimalFormat("#.#####"); 
		DecimalFormat df = new DecimalFormat("#.####"); //to fix luis' 2nd case  
		
		//DecimalFormat df = new DecimalFormat("#.###"); //to fix :Ernest Holloway
		
		String triangleType =  "scalene triangle";
		
		double a = iArrayTriangleSide[0];
		double b = iArrayTriangleSide[1];
		double c = iArrayTriangleSide[2];
		
		if( a == b && c == b )
			triangleType = "equilateral triangle";  //"EQUILATERAL";
	   	else {
	   		
	   		/*
	   		need to implement acute & obtuse
	   		
	   		If the left side is bigger, it's an acute triangle. 
			If the right side is bigger, it's an obtuse triangle. 
			If they are equal, it's a right triangle. 
			(This one gives 676 and 1156, so it is an obtuse triangle)
	   		*/
	   		
	   		System.out.println(df.format(c*c)+","+ df.format((b*b+a*a)) );
	   		
	   		
	   		
		   	System.out.println(a+", "+b+", "+c);
		   	System.out.println(df.format(a)+", "+df.format(b)+", "+df.format(c));

	   		//if(a*a==(b*b+c*c)||b*b==(a*a+c*c)||c*c==(b*b+a*a)){
		   	if(df.format(a*a).equals(df.format( (b*b+c*c)))|| df.format(b*b).equals(df.format((a*a+c*c)))||df.format(c*c).equals(df.format((b*b+a*a)))){ //to make luis' first case true, use 4 decimal point
		   		
		   		if (isRightIsoscle(iArrayTriangleSide)) //modified for Nikolas 2nd case
		   			triangleType = "right isosceles triangle";
		   		else	
		   			triangleType = "right scalene triangle";
	   		}
	   		//else if(((a==b || a==c) && b!=c ) || ((b==a || b==c) && a!=c) || ((c==a || c==b) && a!=b)){
		   	else if( (df.format(a).equals(df.format(b))|| df.format(a).equals(df.format(c)) ) &&  !(df.format(b).equals(df.format(c))) ||
		   			 (df.format(b).equals(df.format(a))|| df.format(b).equals(df.format(c)) ) &&  !(df.format(a).equals(df.format(c))) ||
		   			 (df.format(c).equals(df.format(a))|| df.format(c).equals(df.format(b)) ) &&  !(df.format(a).equals(df.format(b))) ){
		   		
		   		triangleType = "isosceles triangle";
				 
		   		System.out.println( df.format(c/a)+", "+df.format(b/a)+", "+df.format(a/b) +", "+df.format(Math.sqrt(2)));
		   		System.out.println(df.format(a)+", "+ df.format(a)+", "+ df.format(a));
		   		
				if( (a==b && df.format(c/a).equals(df.format(Math.sqrt(2)))) || 
					(a==c && df.format(b/a).equals(df.format(Math.sqrt(2)))) || 
				    (b==c && df.format(a/b).equals(df.format(Math.sqrt(2)))) ) {

						triangleType = "right isosceles triangle";
				}
			}
	   	}
		
		return triangleType;
		
	}
	
	private boolean checkForValidSides(double[] iArrayTSides){
		boolean blnValidSides = true;
		
   		for(double side: iArrayTSides) if(side <= 0) blnValidSides = false;
   		
   		int iLargestPos = findLargest(iArrayTSides);
   		
   		if (iLargestPos == 0){
   			if (iArrayTSides[0] >= iArrayTSides[1]+ iArrayTSides[2])
   				blnValidSides = false;
   		}
   		else if(iLargestPos == 1){
   			if (iArrayTSides[1] >= iArrayTSides[0]+ iArrayTSides[1])
   				blnValidSides = false;

   		}
   		else if(iLargestPos ==2){
   			if (iArrayTSides[2] >= iArrayTSides[0]+ iArrayTSides[1])
   				blnValidSides = false;
   		}
   		
   		return blnValidSides;
	}
	
	private boolean isRightIsoscle(double[] iArrayTSides){
		boolean retVal = false;
		
   		int iLargestPos = findLargest(iArrayTSides);
   		
   		if (iLargestPos == 0){
   			if (iArrayTSides[1] == iArrayTSides[2])
   				retVal = true;
   		}
   		else if(iLargestPos == 1){
   			if (iArrayTSides[0] ==  iArrayTSides[2])
   				retVal = true;		
   		}
   		else if(iLargestPos ==2){
   			if (iArrayTSides[0] == iArrayTSides[1])
   				retVal = true;
   		}

		
		
		return retVal;
		
	
	}
	
	
	
	private int findLargest(double[] dArray)
	{
		
		double largest= dArray[0];
		int iPosition=0;
		
		for(int x=0; x<dArray.length; x++){
			if(dArray[x]>largest){
				largest = dArray[x];
				iPosition = x;
			}
		}
		 
		//System.out.println("Largest no is: "+dArray[iPosition]);
		
		return iPosition;
	}
		 
	/*
	private String checkTriangle(String[] strArraySides) {

		double[] dArgs = new double[strArraySides.length];
		
		for(int i=0; i<strArraySides.length;i++)
		{
			dArgs[i] = Double.parseDouble(strArraySides[i]);
			
		}
		
		return checkTriangle(dArgs);
	}
	*/	
	
	public static void main(String[] args) {
		
		double[] iSides = new double[3];
		
		Triangle tr = new Triangle();
		
		iSides[0] = 1;
		iSides[1] = 1;
		iSides[2] = 2;
		
		tr.validateTriangle(iSides);
		
		
		iSides[0]=1.0;
		iSides[1]=1.0;
		iSides[2]=1.414;
		
		//assertEquals("right isosceles triangle", triangle.validateTriangle(iSides));
		tr.validateTriangle(iSides);
		
		iSides[0] = 2;
		iSides[1] = 2;
		iSides[2] = 2;
		
		//tr.validateTriangle(iSides);
		
		iSides[0] = 3;
		iSides[1] = 4;
		iSides[2] = 5;
		
		//tr.validateTriangle(iSides);
		
		iSides[0]=2.43;
		iSides[1]=3.24;
		iSides[2]=4.05;
		
		//assertEquals("right scalene triangle", triangle.validateTriangle(iSides));
		tr.validateTriangle(iSides);

		
		//Input: 2, 2, 2.828427
		//Expected result: Isosceles right triangle
		iSides[0]=2;
		iSides[1]=2;
		iSides[2]=2.828427;
		tr.validateTriangle(iSides);

		
		iSides[0]=2;
		iSides[1]=2;
		iSides[2]=2.828427;
		
		//assertEquals("right isosceles triangle", triangle.validateTriangle(iSides));
		tr.validateTriangle(iSides);

		
		//luis's 2nd case
		//Input = 1.000005, 1.000020, 3.0 - result: isosceles triangle
		iSides[0]=1.000005;
		iSides[1]=1.000020;
		iSides[2]=1.5;
		
		tr.validateTriangle(iSides);

		
	}

}

